package com.meitianhui.convenitenbuy.module.order.ui.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseFragment;
import com.meitianhui.convenitenbuy.main.adapter.FragmentAdapter;
import com.meitianhui.convenitenbuy.main.model.event.PayJumpBean;
import com.meitianhui.convenitenbuy.util.JumpUtil;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.SystemUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.reactivex.functions.Consumer;

import static com.meitianhui.convenitenbuy.R.id.viewPager;

/**
 * “订单” fragment.
 * Created by Administrator on 2017/10/30.
 */

public class OrderFragment extends BaseFragment {
    public static final String Tag = OrderFragment.class.getSimpleName();
    private String[] mTitles = new String[]{"淘淘领", "自营", "宅急送","会过","积分兑"};
    private ImageView mIvBack, mIvMenu;
    private TextView mTvTitle;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private FragmentAdapter adapter;
    private List<String> mTitleList = new ArrayList<>();
    private List<Fragment> mFragments;
    private boolean showHuiGuo = false;
    private boolean showZiYing = false;

    @Override
    protected int getLayout() {
        return R.layout.fragment_order;
    }

    @Override
    protected void initView(LayoutInflater inflater) {
        super.initView(inflater);
        mIvBack = (ImageView) getViewById(R.id.toolbar_back_all);
        mIvMenu = (ImageView) getViewById(R.id.toolbar_iv_menu);
        RxUtils.bindEvents(mIvMenu, this);
        mTvTitle = (TextView) getViewById(R.id.toolbar_title);
        mTabLayout = (TabLayout) getViewById(R.id.tabLayout);
        mViewPager = (ViewPager) getViewById(viewPager);
        //因为toolbar的背景是白色的，所以他往下顶的高度可以变小一些
        int systemBarHeight = SystemUtils.getSystemBarHeight(getActivity());
        RelativeLayout orderToolBar = (RelativeLayout) getViewById(R.id.base_tool_bar);
        orderToolBar.setPadding(
                orderToolBar.getPaddingLeft(),
                20,
                orderToolBar.getPaddingRight(),
                orderToolBar.getPaddingBottom());

        orderToolBar.getLayoutParams().height = systemBarHeight
                + (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48,
                    getActivity().getResources().getDisplayMetrics());

        initData();
        EventPush();
    }

    // Fixme : 订单生成后，跳转的对应的订单Tab不正确。
    private void EventPush() {
        RxBus.get().toFlowable(PayJumpBean.class).subscribe(new Consumer<PayJumpBean>() {
            @Override
            public void accept(PayJumpBean payJumpBean) throws Exception {
                if (payJumpBean.mType.equals("meitianhui")) {
                    //如果是每天惠的订单
                    mTabLayout.getTabAt(1).getCustomView().setSelected(true);
                    mViewPager.setCurrentItem(1);
                    mTabLayout.getTabAt(1).select();
                    // showZiYing = true;

                } else if (payJumpBean.mType.equals("huiguo")) {
                    // showHuiGuo = true;
                    mTabLayout.getTabAt(3).getCustomView().setSelected(true);
                    mViewPager.setCurrentItem(3);
                    mTabLayout.getTabAt(3).select();
                }
            }
        });
    }

    private void initData() {
        mTvTitle.setText("订单");
        mIvBack.setVisibility(View.GONE);
        mIvMenu.setVisibility(View.VISIBLE);
        mIvMenu.setBackgroundResource(R.mipmap.icon_title_message);
        Collections.addAll(mTitleList, mTitles);

        mFragments = new ArrayList<>();
        mFragments.add(new EdinburghOrderFragment());
        mFragments.add(new OwnSaleOrderFragment());
        // mFragments.add(new BrandOrderFragment());
        mFragments.add(new HomeServiceOrderFragment());
        mFragments.add(new HuiGuoOrderFragment());
        mFragments.add(new BPPOrderFragment());

        adapter = new FragmentAdapter(getChildFragmentManager(), mFragments, mTitleList);
        mViewPager.setAdapter(adapter);//给ViewPager设置适配器
        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来
        mViewPager.setOffscreenPageLimit(4);
        mTabLayout.setSelectedTabIndicatorHeight(0);

        try {
            for (int i = 0; i < mTitleList.size(); i++) {
                //获得到对应位置的Tab
                TabLayout.Tab itemTab = mTabLayout.getTabAt(i);
                if (itemTab != null) {
                    //设置自定义的标题
                    itemTab.setCustomView(R.layout.item_tab_order);
                    TextView textView = (TextView) itemTab.getCustomView().findViewById(R.id.tv_name);
                    textView.setText(mTitleList.get(i));
                }
            }
            mTabLayout.getTabAt(0).getCustomView().setSelected(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                setGone();
                if (tab.getPosition() == 0) {
                    RxView.visibility(getViewById(R.id.iv_edinburgh)).call(true);
                } else if (tab.getPosition() == 1) {
                    RxView.visibility(getViewById(R.id.iv_huiguo_group)).call(true);
                }/*else if(tab.getPosition()==2){
                    RxView.visibility(getViewById(R.id.iv_fight_group)).call(true);
                }*/ else if (tab.getPosition() == 2) {
                    //这个改成自营的
                    RxView.visibility(getViewById(R.id.iv_fight_group)).call(true);
                } else if (tab.getPosition() == 3) {
                    RxView.visibility(getViewById(R.id.iv_home_service)).call(true);
                } else if (tab.getPosition() == 4) {
                    RxView.visibility(getViewById(R.id.iv_bonus_points_payment)).call(true);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        /* //当支付成功或者失败 跳到相对应的页面
        if (showHuiGuo){
            mTabLayout.getTabAt(3).getCustomView().setSelected(true);
            mViewPager.setCurrentItem(3);
            mTabLayout.getTabAt(3).select();
            showHuiGuo = false;
        }
        if (showZiYing){
            mTabLayout.getTabAt(2).getCustomView().setSelected(true);
            mViewPager.setCurrentItem(2);
            mTabLayout.getTabAt(2).select();
            showZiYing = false;
        } */
    }

    @Override
    public void call(View v) {
        switch (v.getId()) {
            case R.id.toolbar_iv_menu:  //消息
                JumpUtil.jumpToChatActivity(getActivity());
                break;
        }
    }

    private void setGone(){
        RxView.visibility(getViewById(R.id.iv_edinburgh)).call(false);
        RxView.visibility(getViewById(R.id.iv_huiguo_group)).call(false);
        RxView.visibility(getViewById(R.id.iv_fight_group)).call(false);
        // RxView.visibility(getViewById(R.id.iv_brand)).call(false);
        RxView.visibility(getViewById(R.id.iv_home_service)).call(false);
        RxView.visibility(getViewById(R.id.iv_bonus_points_payment)).call(false);
    }

}
